// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router';
import store from './store';
import VueLazyload from 'vue-lazyload';
import axios from 'axios';
import ElementUI from 'element-ui';
import VueCookie from 'vue-cookies'
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
// import '../static/css/theme-green/index.css';       // 浅绿色主题
import '../static/css/icon.css';

let params = url_params_decode(location.search);
if (params && params.activity_id) {
  window.activity_id = params.activity_id;
  console.log(params)
}
Vue.use(VueCookie)
Vue.use(ElementUI);
axios.defaults.withCredentials=true;
Vue.prototype.$axios = axios;

Vue.use(VueLazyload, {
  // preLoad: 1.3,
  // error: 'dist/error.png',
  loading: 'static/image/loading-1.svg',
  attempt: 1,
  adapter: {
    error({
      el
    }) {
      el.dispatchEvent(new Event('error'));
    }
  }
});

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  const role = document.cookie
  if (!role && to.path !== '/login') {
    next('/login');
  } else {
    next();
  }
})
Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: {
    App
  },
  template: '<App/>'
});

function url_params_decode(str) {
  if (str) {
    let arr = (/^[?#]/.test(str) ? str.substr(1) : str).split('&');
    if (arr.length) {
      let obj = {};
      for (let i = arr.length; i;) {
        let a = arr[--i].split('=');
        let v = decodeURIComponent(a[1]);
        if (/^[+-]?\d*\.?\d+(e|e\+\d+)?$/g.test(v)) {
          v = parseFloat(v);
        }
        obj[a[0]] = v;
      }
      return obj;
    }
  }
  return null;
}
